package com.braveman.im.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.os.IInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.bean.MyFlockChildBean;
import com.braveman.im.bean.MyFlockGroupBean;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.List;

/**
 * 作者 昊昊.
 * 年份 2017/10/23.
 * 时间 14:02.
 * 项目名称 ZhengQiApp.
 */

/**
 * 我的群适配
 */
public class MyFlockAdapter extends BaseExpandableListAdapter {
    private List<MyFlockGroupBean> list;
    private Context context;

    public MyFlockAdapter(List<MyFlockGroupBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public MyFlockAdapter() {
    }

    @Override
    public int getGroupCount() { //取得父list长度
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {//取得子list长度
        return list.get(groupPosition).children.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(childPosition).children.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        MyFlockGroupHolder holder;
        if (convertView == null) {
            holder = new MyFlockGroupHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_group, null);
            convertView.setTag(holder);
            holder.title = (TextView) convertView.findViewById(R.id.group_title);
        } else {
            holder = (MyFlockGroupHolder) convertView.getTag();
        }
        holder.title.setText(list.get(groupPosition).groupName);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        MyFlockChildHolder childHolder;
        if (convertView == null) {
            childHolder = new MyFlockChildHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_flock_content, null);
            childHolder.img = convertView.findViewById(R.id.activity_img_flock_img);
            childHolder.img.setCircle(false);
            childHolder.title = (TextView) convertView.findViewById(R.id.activity_txt_flock_title);
            childHolder.content = (TextView) convertView.findViewById(R.id.activity_txt_flock_content);
            childHolder.caTegory = (TextView) convertView.findViewById(R.id.activity_txt_flock_caTegory);
            childHolder.affiLiation = (TextView) convertView.findViewById(R.id.activity_txt_flock_affiLiation);
            convertView.setTag(childHolder);
        } else {
            childHolder = (MyFlockChildHolder) convertView.getTag();
        }
        MyFlockChildBean cb = list.get(groupPosition).children.get(childPosition);
        childHolder.img.setImageURI(Uri.parse(cb.imgUrl));
        childHolder.title.setText(cb.tiTle);
        childHolder.content.setText(cb.conTent);
        childHolder.caTegory.setText(cb.caTegory);
        childHolder.affiLiation.setText(cb.affiLiation);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class MyFlockChildHolder {
        QMUIRadiusImageView img;
        TextView title;
        TextView content;
        TextView caTegory; //类别
        TextView affiLiation; //所属平台
    }

    class MyFlockGroupHolder {
        QMUIRadiusImageView iv;
        TextView title;
        TextView number;
    }
}
